using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataViewPage : System.Web.UI.Page
{
    string connectionString =
    WebConfigurationManager.ConnectionStrings[
    "RCVDB2000"].ConnectionString;
    SqlConnection sqlConnection;
    SqlCommand sqlCommand;
    SqlDataAdapter sqlDataAdapter;
    DataTable accountDataTable;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProvince();
            //LoadAccount();        
        }
        LoadAccount();
            
    }
    void LoadProvince()
    {
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString =
        connectionString;
        try
        {
            sqlConnection.Open();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText =
            "select * from tblProvinces";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            sqlDataAdapter
            = new SqlDataAdapter(sqlCommand);
            DataTable dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            FillDataTabletToDropDonList(dataTable);            
            sqlCommand.Dispose();
            sqlConnection.Close();
        }
        catch (Exception ex)
        {
            Literal1.Text += "Failed:" + ex.Message;
        }
        sqlConnection.Dispose();
    }
    void LoadAccount()
    {
        sqlConnection = new SqlConnection();
        sqlConnection.ConnectionString =
        connectionString;
        try
        {
            sqlConnection.Open();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText =            
             "select UserID, FirstName+" +
            " ' ' + LastName as FullName, " +
            " Address,ProvinceID, City from tblJSAccounts";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            sqlDataAdapter
            = new SqlDataAdapter(sqlCommand);
            accountDataTable = new DataTable();
            sqlDataAdapter.Fill(accountDataTable);
             
            LoadData(DropDownList1.SelectedValue );
            sqlCommand.Dispose();
            sqlConnection.Close();
        }
        catch (Exception ex)
        {
            Literal1.Text += "Failed:" + ex.Message;
        }
        sqlConnection.Dispose();
    }
    void LoadData(string selectValue )
    {         
        try
        {             
            DataView dataView = new
            DataView(accountDataTable);
            dataView.RowFilter = 
            "ProvinceID = '" + selectValue + "'";
            GridView1.DataSource = dataView;
            GridView1.DataBind();        
        }
        catch (Exception ex)
        {
            Literal1.Text += "Failed:" + ex.Message;
        }
         
    }
    void FillDataTabletToDropDonList(DataTable dataTable)
    {
        DropDownList1.DataSource = dataTable;
        DropDownList1.DataTextField = "ProvinceName";
        DropDownList1.DataValueField = "ProvinceID";
        DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData(DropDownList1.SelectedValue);
    }
}
